package com.nuo.modules.question.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuo.common.utils.*;
import com.nuo.modules.question.dao.QuestionDao;
import com.nuo.modules.question.entity.QuestionEntity;
import com.nuo.modules.question.po.AddQuestionPo;
import com.nuo.modules.question.service.QuestionService;
import com.nuo.modules.question.vo.QuestionVO;
import com.nuo.modules.student.entity.StudentEntity;
import com.nuo.modules.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service("questionService")
public class QuestionServiceImpl extends ServiceImpl<QuestionDao, QuestionEntity> implements QuestionService {

    @Autowired
    InfoUtils<StudentEntity> infoUtils;

    @Autowired
    StudentService studentService;

    @Override
    public R getList(Map<String, Object> params) {
        StudentEntity student = infoUtils.getInfoByToken((String) params.get("token"), 1, studentService);
        List<QuestionEntity> list = this.list(
                new LambdaQueryWrapper<QuestionEntity>()
                        .eq(QuestionEntity::getStuId, student.getStudentId())
                .orderByDesc(QuestionEntity::getCreateTime)
        );
        return R.ok().put("list", list);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int status = Integer.parseInt((String) params.get("status"));
        IPage page = this.page(
                new Query<QuestionEntity>().getPage(params),
                new LambdaQueryWrapper<QuestionEntity>()
                        .eq(status != -1, QuestionEntity::getStatus, status)
        );
        List<QuestionEntity> questionList = page.getRecords();
        if (questionList == null || questionList.isEmpty() || questionList.size() == 0) {
            return new PageUtils(page);
        }
        List<QuestionVO> vos = BeanUtil.listA2ListB(questionList, QuestionVO.class);
        Set<String> sIdSet = new HashSet<>();
        for (QuestionEntity question : questionList) {
            sIdSet.add(question.getStuId());
        }
        List<StudentEntity> studentList = studentService.list(
                new LambdaQueryWrapper<StudentEntity>()
                        .in(StudentEntity::getStudentId, sIdSet));
        Map<String, StudentEntity> map = new HashMap<>();
        for (StudentEntity student : studentList) {
            map.put(student.getStudentId(), student);
        }
        for (QuestionVO vo : vos) {
            vo.setStuName(map.get(vo.getStuId()).getUsername());
        }
        page.setRecords(vos);
        return new PageUtils(page);
    }

    @Override
    public R add(AddQuestionPo po) {
        StudentEntity student = infoUtils.getInfoByToken(po.getToken(), 1, studentService);
        QuestionEntity question = BeanUtil.beanA2beanB(po, QuestionEntity.class);
        question.setStuId(student.getStudentId());
        save(question);
        return R.ok();
    }

}
